<template>
  <div class="picture-search-view">
    <a-card title="原始图像">
      <a-image :src="route.query.url" style="width: 10vw; object-fit: cover" />
    </a-card>
    <PictureWaterfall v-model="pictureVoList"></PictureWaterfall>

    <a-button @click="nextPage" :loading="loading">继续加载</a-button>
  </div>
</template>

<script lang="ts" setup>
import { onMounted, ref } from 'vue'
import 'vue-waterfall-plugin-next/dist/style.css'
import PictureWaterfall from '@/components/image/PictureWaterfall.vue'
import { message } from 'ant-design-vue'
import { searchSimilarImageByUrlUsingGet } from '@/api/pictureController.ts'
import { useRoute, useRouter } from 'vue-router'

const route = useRoute()
const router = useRouter()
const searchParams = ref<API.SearchPictureByPictureRequest>({
  current: 1,
  pageSize: 8,
  url: '',
})
const loading = ref(false)

const pictureVoList = ref<API.SoImageSearchResult[]>([])
const onSearch = async () => {
  loading.value = true
  searchParams.value.url = route.query.url as string
  if (!searchParams.value?.url) {
    message.error('请输入图片URL')
    router.back()
    return
  }
  const res = await searchSimilarImageByUrlUsingGet({ ...searchParams.value })
  if (res.data.code === 0 && res.data.data) {
    res.data.data.records.forEach((item) => {
      pictureVoList.value.push(item)
    })
    console.log(pictureVoList.value)
  } else {
    router.back()
  }
  loading.value = false
}
const nextPage = () => {
  searchParams.value.current += 1
  onSearch()
}

onMounted(() => {
  onSearch()
})
</script>

<style scoped>
.card-item {
  border-radius: 8px;
  position: relative; /* 确保滚动容器定位正确 */
}

/* 瀑布流样式微调 */
.vue-waterfall-plugin-next-scroll-container {
  padding: 0 10px;
}
</style>
